package com.uenpay.util.widget.selector;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.MotionEvent;
import android.view.View;

import java.lang.reflect.Field;

/**
 * Created by liulou on 2017/5/24.
 * desc:
 */

public class SelectorProcessor implements View.OnTouchListener {
    public void process(final Fragment fragment, View rootView) {
        Class class_ = fragment.getClass();
        Field[] fields = class_.getDeclaredFields();
        for (final Field field : fields) {
            AlphaSelector clickView = field.getAnnotation(AlphaSelector.class);
            if (clickView != null) {
                if (fragment instanceof Fragment) {
                    View view = rootView.findViewById(clickView.value());
                    view.setOnTouchListener(this);
                }
            }
        }
    }

    public void process(final Activity activity) {
        Class class_ = activity.getClass();
        Field[] fields = class_.getDeclaredFields();
        for (final Field field : fields) {
            AlphaSelector clickView = field.getAnnotation(AlphaSelector.class);
            if (clickView != null) {
                if (activity instanceof Activity) {
                    View view = activity.findViewById(clickView.value());
                    view.setOnTouchListener(this);
                }
            }
        }
    }

    public void process(View... views) {
        for (View view : views) {
            view.setOnTouchListener(this);
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                v.setAlpha(0.5f);
                break;
            case MotionEvent.ACTION_MOVE:
                v.setAlpha(0.5f);
                break;
            case MotionEvent.ACTION_UP:
                v.setAlpha(1f);
                break;
            case MotionEvent.ACTION_CANCEL:
                v.setAlpha(1f);
                break;
            default:
                break;
        }
        return false;
    }
}
